﻿namespace HRM.CORE
{
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using HRM.CORE;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using DevExpress.XtraGrid.Views.Base;
    using APP.HELPER.Utils.Language;

    public class xucSalaryMinus : UserControl
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiDelete;
        private GridColumn colDescription;
        private GridColumn colEmployeeCode;
        private GridColumn colIsIncomeTax;
        private GridColumn colMoney;
        private GridColumn colSalaryMinusID;
        private GridColumn colSalaryMinusName;
        private GridColumn colSalaryTableListID;
        private IContainer components;
        private GridView gbList;
        private GridControl gcList;
        private string m_EmployeeCode;
        private decimal m_Money;
        private Guid m_SalaryMinusID;
        private string m_SalaryMinusName;
        private Guid m_SalaryTableListID;
        private PopupMenu ppMenu;
        private RepositoryItemCalcEdit repMoney;

        public event SavedHandler Saved;

        public xucSalaryMinus()
        {
            this.m_SalaryTableListID = Guid.Empty;
            this.m_SalaryMinusID = Guid.Empty;
            this.m_SalaryMinusName = "";
            this.m_Money = 0M;
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
        }

        public xucSalaryMinus(Guid SalaryTableListID, string EmployeeCode)
        {
            this.m_SalaryTableListID = Guid.Empty;
            this.m_SalaryMinusID = Guid.Empty;
            this.m_SalaryMinusName = "";
            this.m_Money = 0M;
            this.m_EmployeeCode = "";
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
            this.Init(this.m_SalaryTableListID = SalaryTableListID, this.m_EmployeeCode = EmployeeCode);
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.gbList.DeleteSelectedRows();
        }

        public void ClearData()
        {
            this.gcList.DataSource = null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void gbList_InitNewRow(object sender, InitNewRowEventArgs e)
        {
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
            if (e.KeyCode == Keys.F5)
            {
                this.Init(this.m_SalaryTableListID, this.m_EmployeeCode);
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            }
        }

        private void gbList_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        public void Init(Guid SalaryTableListID, string EmployeeCode)
        {
            this.barManager1.SetPopupContextMenu(this.gcList, this.ppMenu);
            this.repMoney.EditValueChanging += new ChangingEventHandler(this.repMoney_EditValueChanging);
            this.repMoney.EditValueChanged += new EventHandler(this.repMoney_EditValueChanged);
            this.gcList.DataSource = new HRM_SALARY_MINUS().GetList(this.m_SalaryTableListID = SalaryTableListID, this.m_EmployeeCode = EmployeeCode);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.gcList = new GridControl();
            this.gbList = new GridView();
            this.colSalaryTableListID = new GridColumn();
            this.colEmployeeCode = new GridColumn();
            this.colSalaryMinusID = new GridColumn();
            this.colSalaryMinusName = new GridColumn();
            this.colMoney = new GridColumn();
            this.repMoney = new RepositoryItemCalcEdit();
            this.colIsIncomeTax = new GridColumn();
            this.colDescription = new GridColumn();
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.bbiDelete = new BarButtonItem();
            this.ppMenu = new PopupMenu(this.components);
            this.gcList.BeginInit();
            this.gbList.BeginInit();
            this.repMoney.BeginInit();
            this.barManager1.BeginInit();
            this.ppMenu.BeginInit();
            base.SuspendLayout();
            this.gcList.Dock = DockStyle.Fill;
            this.gcList.EmbeddedNavigator.Buttons.Append.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Edit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
            this.gcList.EmbeddedNavigator.Buttons.Remove.Visible = false;
            this.gcList.EmbeddedNavigator.TextStringFormat = "{0}/{1}";
            this.gcList.Location = new Point(0, 0);
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.repMoney });
            this.gcList.Size = new Size(0x34c, 0x16a);
            this.gcList.TabIndex = 5;
            this.gcList.UseEmbeddedNavigator = true;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.gbList.Appearance.GroupRow.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
            this.gbList.Appearance.GroupRow.Options.UseFont = true;
            this.gbList.BorderStyle = BorderStyles.NoBorder;
            this.gbList.Columns.AddRange(new GridColumn[] { this.colSalaryTableListID, this.colEmployeeCode, this.colSalaryMinusID, this.colSalaryMinusName, this.colMoney, this.colIsIncomeTax, this.colDescription });
            this.gbList.GridControl = this.gcList;
            this.gbList.GroupPanelText = "K\x00e9o v\x00e0 Thả cột v\x00e0o đ\x00e2y để nh\x00f3m dữ liệu";
            this.gbList.IndicatorWidth = 40;
            this.gbList.Name = "gbList";
            this.gbList.OptionsBehavior.AllowIncrementalSearch = true;
            this.gbList.OptionsBehavior.AutoExpandAllGroups = true;
            this.gbList.OptionsBehavior.CopyToClipboardWithColumnHeaders = false;
            this.gbList.OptionsBehavior.FocusLeaveOnTab = true;
            this.gbList.OptionsLayout.StoreAllOptions = true;
            this.gbList.OptionsLayout.StoreAppearance = true;
            this.gbList.OptionsMenu.EnableColumnMenu = false;
            this.gbList.OptionsMenu.EnableGroupPanelMenu = false;
            this.gbList.OptionsNavigation.EnterMoveNextColumn = true;
            this.gbList.OptionsSelection.InvertSelection = true;
            this.gbList.OptionsSelection.MultiSelect = true;
            this.gbList.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect;
            this.gbList.OptionsView.ColumnAutoWidth = false;
            this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.gbList.OptionsView.RowAutoHeight = true;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.InitNewRow += new InitNewRowEventHandler(this.gbList_InitNewRow);
            this.gbList.KeyDown += new KeyEventHandler(this.gbList_KeyDown);
            this.colSalaryTableListID.Caption = "M\x00e3 bảng lương";
            this.colSalaryTableListID.FieldName = "SalaryTableListID";
            this.colSalaryTableListID.Name = "colSalaryTableListID";
            this.colEmployeeCode.Caption = "M\x00e3 nh\x00e2n vi\x00ean";
            this.colEmployeeCode.FieldName = "EmployeeCode";
            this.colEmployeeCode.Name = "colEmployeeCode";
            this.colEmployeeCode.OptionsColumn.AllowEdit = false;
            this.colEmployeeCode.OptionsColumn.ReadOnly = true;
            this.colEmployeeCode.Width = 0x4d;
            this.colSalaryMinusID.Caption = "M\x00e3 thu nhập";
            this.colSalaryMinusID.FieldName = "SalaryMinusID";
            this.colSalaryMinusID.Name = "colSalaryMinusID";
            this.colSalaryMinusID.Width = 0x75;
            this.colSalaryMinusName.Caption = "Loại thu nhập trừ v\x00e0o lương";
            this.colSalaryMinusName.FieldName = "SalaryMinusName";
            this.colSalaryMinusName.Name = "colSalaryMinusName";
            this.colSalaryMinusName.Visible = true;
            this.colSalaryMinusName.VisibleIndex = 0;
            this.colSalaryMinusName.Width = 340;
            this.colMoney.Caption = "Số tiền (VND)";
            this.colMoney.ColumnEdit = this.repMoney;
            this.colMoney.FieldName = "Money";
            this.colMoney.Name = "colMoney";
            this.colMoney.Visible = true;
            this.colMoney.VisibleIndex = 1;
            this.colMoney.Width = 0x83;
            this.repMoney.AutoHeight = false;
            this.repMoney.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repMoney.Mask.UseMaskAsDisplayFormat = true;
            this.repMoney.Name = "repMoney";
            this.colIsIncomeTax.Caption = "Trước thuế";
            this.colIsIncomeTax.FieldName = "IsIncomeTax";
            this.colIsIncomeTax.Name = "colIsIncomeTax";
            this.colIsIncomeTax.Visible = true;
            this.colIsIncomeTax.VisibleIndex = 2;
            this.colIsIncomeTax.Width = 90;
            this.colDescription.Caption = "Ghi ch\x00fa";
            this.colDescription.FieldName = "Description";
            this.colDescription.Name = "colDescription";
            this.colDescription.Visible = true;
            this.colDescription.VisibleIndex = 3;
            this.colDescription.Width = 0x112;
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiDelete });
            this.barManager1.MaxItemId = 1;
            this.bbiDelete.Caption = "X\x00f3a";
            this.bbiDelete.Id = 0;
            this.bbiDelete.Name = "bbiDelete";
            this.bbiDelete.ItemClick += new ItemClickEventHandler(this.bbiDelete_ItemClick);
            this.ppMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiDelete) });
            this.ppMenu.Manager = this.barManager1;
            this.ppMenu.Name = "ppMenu";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
//            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.gcList);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucSalaryMinus";
            base.Size = new Size(0x34c, 0x16a);
            this.gcList.EndInit();
            this.gbList.EndInit();
            this.repMoney.EndInit();
            this.barManager1.EndInit();
            this.ppMenu.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            this.colSalaryMinusName.Caption = MultiLanguages.GetString("tbl_SalaryMinus", "SalaryMinusType", this.colSalaryMinusName.Caption);
            this.colMoney.Caption = MultiLanguages.GetString("tbl_SalaryMinus", "Money", this.colMoney.Caption);
        }

        private void RaiseSavedHander()
        {
            if (this.Saved != null)
            {
                this.Saved(this);
            }
        }

        private void repMoney_EditValueChanged(object sender, EventArgs e)
        {
        }

        private void repMoney_EditValueChanging(object sender, ChangingEventArgs e)
        {
            try
            {
                this.m_Money = decimal.Parse(e.NewValue.ToString());
            }
            catch
            {
            }
        }

        public void Save()
        {
            HRM_SALARY_MINUS hrm_salary_minus = new HRM_SALARY_MINUS();
            hrm_salary_minus.Delete(this.m_SalaryTableListID, this.m_EmployeeCode);
            try
            {
                foreach (DataRow row in (this.gcList.DataSource as DataTable).Rows)
                {
                    try
                    {
                        if (row != null)
                        {
                            bool isIncomeTax = false;
                            try
                            {
                                isIncomeTax = bool.Parse(row["IsIncomeTax"].ToString());
                            }
                            catch
                            {
                            }
                            hrm_salary_minus.Insert(this.m_SalaryTableListID, this.m_EmployeeCode, Guid.NewGuid(), row["SalaryMinusName"].ToString(), decimal.Parse(row["Money"].ToString()), isIncomeTax, row["Description"].ToString());
                        }
                    }
                    catch
                    {
                    }
                }
                this.UpdateEmployeeSalary();
                this.RaiseSavedHander();
            }
            catch
            {
            }
        }

        private void UpdateEmployeeSalary()
        {
            HRM_TIMEKEEPER_TABLELIST hrm_timekeeper_tablelist = new HRM_TIMEKEEPER_TABLELIST();
            HRM_SALARY_TABLELIST hrm_salary_tablelist = new HRM_SALARY_TABLELIST();
            hrm_salary_tablelist.GetByID(this.m_SalaryTableListID);
            hrm_timekeeper_tablelist.Get(hrm_salary_tablelist.Month, hrm_salary_tablelist.Year);
            HRM_SALARY.EmployeeUpdate(hrm_timekeeper_tablelist.TimeKeeperTableListID.ToString(), this.m_SalaryTableListID.ToString(), hrm_salary_tablelist.Month, hrm_salary_tablelist.Year, this.m_EmployeeCode);
        }

        public delegate void SavedHandler(object sender);
    }
}

